package cn.com.aiton.common.utils;

import cn.com.aiton.common.constant.Gbt209992007;
import cn.com.aiton.common.model.extend.AitonExtendControlerStateDomain;
import cn.com.aiton.common.model.extend.AitonExtendCountDownDomain;
import org.apache.commons.lang3.ArrayUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class AitonExtendBytesToObjectUtils {


    /**
     * 取得信号机的状态信息，控制板功能
     * @param bytes
     * @return
     */
    public static  AitonExtendControlerStateDomain bytes2Control(byte[] bytes) {
        AitonExtendControlerStateDomain aitonExtendControlerStateDomain = new AitonExtendControlerStateDomain();
            byte[] temperature = new byte[]{bytes[3] ,bytes[4]};
            Short tem = ByteUtils.byteToShort(temperature);
            aitonExtendControlerStateDomain.setTemperature(tem.intValue());
            Byte door = bytes[5];
            aitonExtendControlerStateDomain.setDoor(door.intValue());
            byte[] voltage = new byte[]{bytes[6] ,bytes[7]};
            Short vol = ByteUtils.byteToShort(temperature);
            aitonExtendControlerStateDomain.setVoltage(vol.intValue());
            Byte type = bytes[8];
            aitonExtendControlerStateDomain.setElectricType(type.intValue());
            return aitonExtendControlerStateDomain;
    }


    /**
     * 取得倒计时配置数据
     * @param bytes
     * @return
     */
    public  static List<AitonExtendCountDownDomain> byte2CountDown(byte[] bytes) {
        List<AitonExtendCountDownDomain> models = new ArrayList<>();

            byte[] objectArray = new byte[bytes[3] * Gbt209992007.ECHO_COUNT_DOWN_BYTE_SIZE];
            System.arraycopy(bytes, 4, objectArray, 0, objectArray.length);
            byte[][] countDownArrayResult = ByteUtils.oneArrayToTwoArray(objectArray, Gbt209992007.ECHO_COUNT_DOWN_RESULT_LEN, Gbt209992007.ECHO_COUNT_DOWN_BYTE_SIZE);
            for (int i = 0; i < bytes[3]; i++) {
                AitonExtendCountDownDomain model = new AitonExtendCountDownDomain();
                model.setCountDownNumber(Integer.valueOf(countDownArrayResult[i][0]));
                byte[] bytes1 = {countDownArrayResult[i][1], countDownArrayResult[i][2]};
                model.setPhase(ByteUtils.byteToInt(bytes1));
                model.setOverlapPhase(ByteUtils.bytesUInt(countDownArrayResult[i][3]));
                model.setMode(ByteUtils.byteToInt(countDownArrayResult[i][4]));
                models.add(model);
            }

            return models;

    }

    /**
     * 设置倒计进数据信息
     * @param countDowns
     * @return
     */
    public static byte[] countDown2Byte(List<AitonExtendCountDownDomain> countDowns) {

            byte[] hex = ArrayUtils.add(Gbt209992007.SET_CNTDOWNDEV_RESPONSE, (byte) countDowns.size());
            Iterator<AitonExtendCountDownDomain> countDownIterator = countDowns.iterator();
            while (countDownIterator.hasNext()) {
                AitonExtendCountDownDomain command = countDownIterator.next();
                hex = ArrayUtils.add(hex, ByteUtils.intToByteOne(command.getCountDownNumber()));
                hex = ArrayUtils.add(hex, command.getPhase().byteValue());
                hex = ArrayUtils.add(hex, command.getOverlapPhase().byteValue());
                hex = ArrayUtils.add(hex, ByteUtils.intToByteOne(command.getMode()));
            }
        return hex;
    }
}
